<template>
	<view class="change-password">
		<view class="top-bg"></view>
		<view class="pl-3 pr-3 change-password-wrap">
			<view class="pt-1 pb-2 pl-3 pr-3 change-password-panel">
			<u--form class="mb-3 forgot-password-form" labelWidth="70" :borderBottom="false" :model="cpForm"
				:rules="cpRules" ref="cpFormRef">
				<u-form-item label="原密码" prop="OldPwd" :borderBottom="true">
					<u--input password v-model="cpForm.OldPwd" placeholder="请输入原密码" border="none" clearable></u--input>
				</u-form-item>
				<u-form-item label="新密码" prop="NewPwd" :borderBottom="true">
					<u--input password v-model="cpForm.NewPwd" placeholder="请输入6-20位的密码" border="none" clearable></u--input>
				</u-form-item>
				<u-form-item label="确认密码" prop="NewPwd2" :borderBottom="true">
					<u--input password v-model="cpForm.NewPwd2" placeholder="请确认新密码" border="none" clearable></u--input>
				</u-form-item>

			</u--form>
			<u-button @click="handleResetPassword" color="#007aff" shape="circle" text="保存"></u-button>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		changePwd
	} from "@/apis/user.js";

	export default {
		data() {
			return {
				cpForm: {
					OldPwd: '',
					NewPwd: '',
					NewPwd2: '',

				},
				cpRules: {
					OldPwd: {
						required: true,
						message: '请输入原密码',
						trigger: ['blur', 'change']
					},
					NewPwd: {
						min: 6,
						max: 20,
						required: true,
						message: '请输入6-20位的密码',
						trigger: ['blur', 'change']
					},
					NewPwd2: [{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								return value == this.cpForm.NewPwd;
							},
							message: '两次输入密码不一致',
							trigger: ['change', 'blur'],
						}
					],

				},
			};
		},
		onLoad() {

		},
		methods: {
			async handleResetPassword() {
				if (!await this.$refs.cpFormRef.validate()) return;
				console.log('this.fpForm',this.cpForm)
				const cpData = this.cpForm
				const res = await changePwd(cpData)
				console.log('?', res)
				if (res.MsgCode == 0) {
					uni.showToast({
						title: '修改密码成功，请重新登录!',
						duration: 2000,
						icon: 'none'
					});
					setTimeout(()=>{
						uni.removeStorageSync('accountInfo');
						uni.reLaunch({
							url:'/pages/login/login'
						})
					},2000)
				} else {
					return uni.showToast({
						title: res.ErrMsg,
						duration: 3000,
						icon: 'none'
					});
				}
			},
		},
	}
</script>

<style lang="scss">
	.change-password {
		.top-bg {
			width: 100%;
			height: 120rpx;
			background-color: #007aff;
			z-index: -1;
		}

		.change-password-wrap {
			margin-top: -72rpx;

		.change-password-panel{
			background-color: #fff;
			border-radius: $s-border-radius;
			box-shadow: $s-bg-shadow;
		}

		}
	}
</style>
